package com.insigma.business.qrcode.soldier.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.logging.log4j.util.Strings;

import java.util.List;


public class NativeImgUtils {

    private static String prefix = "https://zhlb.tyjrswj.hangzhou.gov.cn/img/gongwuyuann/";

    public static String convertToFullPath(String relativePath) {
        if (Strings.isBlank(relativePath)) {
            return null;
        }

        List<String> result = Lists.newArrayList();

        String[] split = relativePath.split(",");
        for (String item : split) {
            if (!item.toLowerCase().startsWith("http")) {
                item = prefix + item;
            }
            result.add(item);
        }
        return String.join(",", result);
    }


    public static JSONArray convertToYidaObject(String relativePath) {
        if (Strings.isBlank(relativePath)) {
            return null;
        }

        JSONArray jsonArray = new JSONArray();
        for (String item : convertToFullPath(relativePath).split(",")) {

            String downloadUrl = item;
            String name = "yida";
            String previewUrl = item;
            String url = item ;

            String ext = getExt(item);
            JSONObject object = new JSONObject();
            object.put("downloadUrl", downloadUrl);
            object.put("name", name);
            object.put("previewUrl", previewUrl);
            object.put("url", url);
            object.put("ext", ext);
            jsonArray.add(object);
        }

        return jsonArray;
    }

    private static String getExt(String item) {
        int i = item.lastIndexOf(".");
        String substring = item.substring(i + 1, item.length());
        return substring;
    }


}
